# -*- coding:utf-8 -*-
# @Time: 2020-08-29 17:58
# @Author: aniu
# @File: jsonUtil.py

from flask import jsonify
from web.common.file import writeFile
'''
    数据请求返回方法
'''


def responseJson(status, object, msg):
    '''
    object 转换为字符串
    :param status:  状态
    :param object: 变量对象
    :param msg: 字符串
    :return:
    '''
    if isinstance(object, list):
        fields = []
        for item in object:
            record = {}
            record = props(item)
            fields.append(record)
        return parseData(status, fields, msg)
    elif isinstance(object, dict):
        return parseData(status, object, msg)
    else:
        record = {}
        record = props(object)
        return parseData(status, record, msg)


def props(object):
    '''
     object 转换为dict
    :param object:
    :return:
    '''
    pro = {}
    for name in dir(object):
        value = getattr(object, name)
        if not name.startswith('__') and not name.startswith('_') and not callable(value) and not hasattr(
            object.__getattribute__(name),'__call__') and name != 'metadata' and name != 'query':
            pro[name] = value

    return pro


#
#json格式输出
#
def parseData(status, data, msg):
    json = {'status': status, 'data': data, 'msg': msg}
    return jsonify(json)